#include "rfid_api.h"
#include "rfid_board.h"
#include "rfid_detection.h"

#if _RFID_FUNCTION_ENABLE_

void RfidExitCardDetectionMode(void)//退出卡检测模式Davy
{
  RfidDisableInterrupt();
  RfidSetResetPinLevel(true);
}

void RfidEnterCardDetectionMode(void)
{
  RfidSetResetPinLevel(true);

  if (RfidSoftwareReset() == true)
  {
    RfidPcdSetBitMask(JREG_COMMAND, 0x20);

    uint8_t RegValue = RfidPcdReadReg(JREG_COMMAND);
    if (RegValue == 0x20)
    {
      uint16_t CardReadingSensitivity = RfidGetCardReadingSensitivity();

      RfidWriteExtReg(0x24, 0x14);
      RfidWriteExtReg(0x25, 0x3A);
      RfidWriteExtReg(JREG_LPCDCTRLMODE, (RF_DET_ENABLE | RF_DET_SEN_00 | LPCD_ENABLE));

      RfidWriteExtReg(JREG_LPCDSLEEPTIMER, LPCD_SLEEPTIME);
      RfidWriteExtReg(JREG_LPCDRFTIMER, LPCD_IRQINV_ENABLE | LPCD_IRQ_PUSHPULL | LPCD_RFTIME_5us);
      RfidWriteExtReg(JREG_LPCDTHRESH_H, (uint8_t)((CardReadingSensitivity & 0xFF00) >> 8));
      RfidWriteExtReg(JREG_LPCDTHRESH_L, (uint8_t)(CardReadingSensitivity & 0x00FF));
      RfidWriteExtReg(JREG_LPCDTXCTRL2, LPCD_CWP);
      RfidWriteExtReg(JREG_LPCDTXCTRL3, LPCD_CWN);

      RfidWriteExtReg(JREG_LPCDREQATIMER, LPCD_REQA_TIME_100us);
      RfidWriteExtReg(JREG_LPCDREQAANA, LPCD_RXGAIN_33DB | LPCD_MINLEVEL_9 | LPCD_MODWIDTH_38);
      RfidWriteExtReg(JREG_LPCDDETECTMODE, LPCD_TXSCALE_4 | LPCD_RX_CHANGE_MODE);
    }
  }

  RfidEnableInterrupt();
  RfidSetResetPinLevel(false);

  PRINT("[rfid]: into detection mode\r\n");
}

#endif
